package com.quarters.controller.wx;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quarters.bean.Inquiry;
import com.quarters.bean.Quarters;
import com.quarters.bean.Repair;
import com.quarters.bean.User;
import com.quarters.commen.BaseController;
import com.quarters.util.Respons.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * 报修查询表 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-02
 */
@Controller
@RequestMapping("wx/inquiry")
public class WXInquiryController extends BaseController {


    /**
     * @param param
     * @return 添加
     * userId 当前用户的id
     */
    @RequestMapping(value = "saveInquiry",method = RequestMethod.POST)
    @ResponseBody
    public Map saveInquiry(String param,HttpServletRequest request){
        User user1 = userService.getById(getUser(request).getId());
        if(user1.getQuartersId()==null && user1.getDetailedFloor()==null){
            return ResponseUtil.RequestException("请先绑定小区在提交申请!");
        }else{
            Inquiry inquiry = JSON.parseObject(param, Inquiry.class);
            System.out.println("inquiry:"+inquiry);
            User user = userService.getById(inquiry.getUserid());
            System.out.println("user:"+user);
            inquiry.setCreateTime(System.currentTimeMillis());
            if(inquiryService.save(inquiry)){
                //给标签管理员发送报修模板消息
                //标签管理员的oppenid 和id
                //报修类型
                //查询报修类型
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("id",inquiry.getRepairId());
                Repair repair = repairService.getOne(queryWrapper);
                System.out.println("repair:"+repair);
                inquiryService.paySuccess(repair.getName(),user.getId(),inquiry.getCreateTime(),inquiry.getRepairContent(),inquiry.getPhone());
                return ResponseUtil.getSuccessMap();
            }
            return ResponseUtil.getExceptionMap();
        }
    }
}
